class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n=arr.size();
        unordered_map<int,int> hash(n);
        for(int i=0;i<n;++i)
        {
            hash[arr[i]]=i;
        }
        int ret=1;
        vector<vector<int>> dp(n,vector<int>(n,2));
        for(int j=2;j<n;++j)
        {
            for(int i=j-1;i>0;--i)
            {
                int x=arr[j]-arr[i];
                if(hash.count(x)&&hash[x]<i)
                {
                    dp[i][j]=dp[hash[x]][i]+1;
                }
                ret=max(ret,dp[i][j]);
            }
        }
        if(ret<3) return 0;
        return ret;
    }
};
